#!/usr/bin/env python
# encoding=utf-8

import sys

from elasticsearch import Elasticsearch
from elasticsearch import helpers
from pymongo import MongoClient
from pypinyin import lazy_pinyin
from scpy.logger import get_logger
from xtls.codehelper import forever

reload(sys)
sys.setdefaultencoding('utf-8')

logger = get_logger(__file__)
MONGO_HOST = '10.132.23.104'
LIMIT = 5000
INDEX = 'company_info_20160325'
hosts = ['10.117.61.31', '10.162.81.30', '10.161.163.247']


def create_index():
    es = Elasticsearch(hosts)
    es.indices.create(index=INDEX, ignore=400)


def zh_2_py(string):
    return ' '.join(lazy_pinyin(string)).lower()


def main():
    es = Elasticsearch(hosts=hosts)
    conn = MongoClient(MONGO_HOST, 27017)
    coll = conn['organization']['companyNameStrict_2015_12']

    last_id, count, item = '', 0, {}
    for loop_count in forever(1):
        logger.info('important: last_id="%s"' % last_id)

        actions = []

        for index, item in enumerate(coll.find({'_id': {'$gt': last_id}}).sort([('_id', 1)]).limit(LIMIT), start=1):
            logger.info('now : {index}-{company}'.format(index=index, company=item['companyName']))
            chn = zh_2_py(item["companyName"])
            data = {
                "companyName": item["companyName"],
                "location": item["location"],
                "regNo": item["regNo"],
                "province": item["province"],
                "orgNo": item["_id"],
                "zfrq": item["zfrq"],
                "registOrg": item["registOrg"],
                "registTime": item["registTime"],
                'englishName': chn,
                'nameAlias': u'%s %s' % (item["companyName"], chn),
                'addressAlias': u'%s %s' % (item["companyName"], zh_2_py(item["location"])),
            }
            eng_item = conn['alibaba']['etoc_one'].find_one({'orgNo': data['orgNo']})
            if eng_item:
                data['englishName'] = eng_item['englishName']
            if not data['englishName']:
                data['englishName'] = chn
            actions.append({
                "_id": item["companyName"],
                "_index": INDEX,
                "_source": data,
                "_type": "company-info"
            })
        helpers.bulk(es, actions)
        if index != LIMIT:
            break
        last_id = item['_id']
        logger.info('loop_count: %s, dealed: %s' % (loop_count, loop_count * LIMIT))


if __name__ == '__main__':
    main()
